package com.qdlc.p2p.biz.service.lottery.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.qdlc.p2p.biz.processor.event.LotteryValueEvent;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.service.lottery.AbstractDrawLotteryService;
import com.qdlc.p2p.biz.service.lottery.LotteryAlgorithmFactory;
import com.qdlc.p2p.biz.service.lottery.LotteryTaskService;
import com.qdlc.p2p.biz.zdata.LotteryResult;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.enums.EnumActicityPlanNid;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.dal.dao.AdditionalUserDrawLotteryDao;
import com.qdlc.p2p.dal.dao.LotteryDao;
import com.qdlc.p2p.dal.dto.AdditionalUserDrawLottery;
import com.qdlc.p2p.dal.dto.Lottery;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.model.BaseGiveAwardModel;
import com.qdlc.p2p.dal.model.UserRedEnvelopeModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * Created by hongbl on 2016/2/2.
 * 抽奖次数抽奖
 *
 */
@Service("countDrawLotteryService")
public class CountDrawLotteryService implements AbstractDrawLotteryService{

    /** 积分每次花费 */
    private static final int COUNT_DRAW_CONSUME = 1;

    @Resource
    private AdditionalUserDrawLotteryDao additionalUserDrawLotteryDao;
    @Resource
    private UserIdentifyService userIdentifyService;
    @Resource
    private UserService userService;
    @Resource
    private LotteryDao lotteryDao;
    @Resource
    private LotteryTaskService lotteryTaskService;


    /**
     * 抽奖过程 将从前分步的做法现在合成一步
     * 1 进行抽奖资格的验证
     * 2 进行抽奖
     * 3 颁奖以及记录
     */
    public LotteryResult process(User user,int category){
        if (user == null) throw new UserException("用户不存在,请重新登录!", BusinessException.TYPE_JSON);
         //取得用户剩余抽奖次数
        AdditionalUserDrawLottery additionalUserDrawLottery = additionalUserDrawLotteryDao.getValidDrawLotteryByUserId(user.getUserId());
        int userCount = additionalUserDrawLottery.getCount();
        if(COUNT_DRAW_CONSUME> userCount){
            throw new UserException("您已没有抽奖机会!", BusinessException.TYPE_JSON);
        }
        //验证有没有开通托管账户或者三方账户是否为空
        UserIdentify userIdentify = userIdentifyService.findByUserId(user.getUserId());
        if (null != userIdentify && userIdentify.getRealNameStatus() != 1 || null == userIdentify|| StringUtils.isBlank( UserUtils.getUserTppCustId(userService.find(user.getUserId()).getUserId()))){
            throw new UserException("您还没有通过实名认证，请先通过实名认证！", BusinessException.TYPE_JSON);
        }
        //更新抽奖次数
        additionalUserDrawLottery.setCount(userCount - COUNT_DRAW_CONSUME);
        additionalUserDrawLotteryDao.update(additionalUserDrawLottery);

        //抽奖
        List<Lottery> lotteryConfig = lotteryDao.getLotteryByCategory(category);

        //根据抽奖配置以及算法得到结果
        LotteryResult lotteryResult = LotteryAlgorithmFactory.getResult(lotteryConfig);

        // 发放红包，加息券 填写相应的发放记录
        BaseGiveAwardModel baseGiveAwardModel = new BaseGiveAwardModel(user, lotteryResult.getValue(), EnumActicityPlanNid.LOTTERY.getValue(), lotteryResult.getValidDay(), "签到抽奖");
        baseGiveAwardModel.setRuleId(lotteryResult.getItemId());
        //6:现金红包，9:虚拟红包
        if (lotteryResult.getType() == 6) {
            baseGiveAwardModel.setRuleType(UserRedEnvelopeModel.USER_RED_ENVELOPE_TYPE_CASH);
        } else if (lotteryResult.getType() == 9) {
            baseGiveAwardModel.setRuleType(UserRedEnvelopeModel.USER_RED_ENVELOPE_TYPE_VIRTUAL);
        }

        //保存中奖日志
        lotteryTaskService.saveLotteryLog(lotteryResult,user.getUserId());

        // 颁奖业务逻辑,放进队列中执行
        LotteryValueEvent lotteryEvent = new LotteryValueEvent();
        lotteryEvent.setUser(user);
        lotteryEvent.setLotteryResult(lotteryResult);
        lotteryEvent.setBaseGiveAwardModel(baseGiveAwardModel);
        QueueHandlers.submit(lotteryEvent);
        return lotteryResult;
    }
}
